(function(){
    angular.module('app').controller("dspAgeReportCtrl",function($scope,proxy){

        $scope.sTime = getNowFormatDate() + " 00:00:00";
        $scope.eTime = getNowFormatDate() + " 23:59:59";
        $scope.chooseDate = function (start, end) {
            if(start) {
                $scope.sTime = start + " 00:00:00";
                $scope.eTime = end + " 23:59:59";
            } else {
                $scope.sTime = $scope.eTime = undefined;
            }
            getList();
            getSum();
        }
        
        $scope.filter = {};
        $scope.search = function() {
            getList();
            getSum();
        }
        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                'filter': {},
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agency',
                data: data,
                success: function(res){
                    var arr = res.data.data || [];
                    arr.forEach(function (v) {
                        v.dspProfitRate = (v.dspProfitRate * 100).toFixed(2) + "%";
                        v.clickRate = (v.clickRate * 100).toFixed(2) + "%";
                    })
                    exportCsv({
                        title: ['广告公司名称','广告公司花费(元)','DSP成本(元)','利润(元)','利润率','展示量','点击量','点击率','CPM单价(元)','CPC单价(元)'],
                        titleForKey: ['agencyName','agencyCost','dspCost','dspProfit','dspProfitRate','view','click','clickRate','cpm','cpc'],
                        data: arr,
                    },"广告公司报表.csv");
                }
            })
        }

        getList();
        getSum();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': $scope.filter,
                "sort": ["agencyCost,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agency',
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data ;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }

        function getSum() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agency/sum',
                load: true,
                data: {
                    'page': "1",
                    'size': "999999",
                    'filter': $scope.filter,
                    sTime : $scope.sTime,
                    eTime : $scope.eTime
                },
                success: function(res){
                    // 获得列表
                    $scope.total = res.data.data;
                }
            })
        }
    })
})();